package com.sali.disruptor;

import com.lmax.disruptor.WorkHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadFactory;

/**
 * @description:
 * @author: sali
 * @date: 2025/2/22 下午2:54.
 * @version: 1.0
 */
@Slf4j
public class MessageDisruptorService extends BaseDisruptor {

    private int consumerNum;

    public MessageDisruptorService(int waitStrategy, int ringBufferSize, int consumerNum, ThreadFactory threadFactory) {
        super(waitStrategy, ringBufferSize, threadFactory);
        this.consumerNum = consumerNum;
    }

    @Override
    public List<WorkHandler> getConsumer() {
        List<WorkHandler> workHandlers = new ArrayList<>(consumerNum);
        for (int i = 0; i < consumerNum; i++) {
            MessageConsumer messageConsumer = MessageConsumer.builder().build();
            workHandlers.add(messageConsumer);
        }
        return workHandlers;
    }

    /**
     * 释放资源
     */
    public void destory() {
        log.info("开始关闭disruptor组件.....");
        this.getDisruptorComponent().distroy();
        log.info("释放disruptor组件完毕.....");
    }
}
